package com.ywb.ex09;

import com.ywb.ex09.tools.MyTools;
import com.ywb.ex09.view.PetAdminDialogController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainFrame extends JFrame {
    //公共组件的声明定义部分
    private  ImageIcon  image=new ImageIcon("src/gui/layout/img/a3.jpg");
    private  JLabel imgJLable=new JLabel(image);
    private  JButton btn=new JButton("进入系统");
    private JMenuBar jmb=new JMenuBar();
    private JMenu  file=new JMenu( "文件");
    private JMenuItem item1=new JMenuItem("打开");
    private JMenuItem item2=new JMenuItem("退出");
    private JPopupMenu p1;

    public MainFrame(){
        //初始化操作
        this.init();
        //添加组件
        this.addComponent();
        //添加监听器
        this.addListener();
        //添加菜单
        this.addMenu();
    }
    private void addMenu(){

        jmb.add(file);
        item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, InputEvent.CTRL_MASK));
        file.add(item1);
        file.add(item2);
        p1=file.getPopupMenu();
    }
    /**
     * 添加监听器
     */
    private  void addListener(){
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                 //进入宠物信息管理主窗口
               new PetAdminDialogController().setVisible(true);

            }
        });

        item2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        item1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //进入宠物信息管理主窗口
                new PetAdminDialogController().setVisible(true);

            }
        });

        imgJLable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                super.mouseReleased(e);
                if(e.getButton()==MouseEvent.BUTTON3)
                    p1.show(file,e.getX(),e.getY());
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(e.getButton()==MouseEvent.BUTTON1)
                    p1.setVisible(false);
            }
        });
    }
    /**
     * 添加组件
     */
    private void addComponent(){
        this.add(imgJLable, BorderLayout.NORTH);
        JPanel btnJPanel=new JPanel();
        btnJPanel.setLayout(null);
        this.add(btnJPanel);
        btn.setBounds(300,20,120,50);
        btnJPanel.add(btn);
    }
    /**
     * 初始化操作
     */
    private void init(){
        setTitle("宠物商店管理系统");
        setSize(700,580);
        setLocationRelativeTo(null);
        setResizable(false);
        MyTools.setTitleImage(this,"src/gui/layout/img/icon4.png");
        setJMenuBar(jmb);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[]args){
        new MainFrame();
    }
}
